package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYfindClubPartnerReq;
import com.game.server.message.proto.out.XYclubPartnerStruct;
import com.game.server.message.proto.out.XYclubplayerStruct;
import com.game.server.message.proto.out.XYfindClubPartnerRsp;
import com.game.server.message.proto.out.XYmClubplayersRsp;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class XYfindClubPartnerReq extends AbstractXYfindClubPartnerReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            int limit = 15;
            XYfindClubPartnerRsp rsp = new XYfindClubPartnerRsp();
            //获取俱乐部信息
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid.intValue());
            if (clubInfo!=null){
                XyClubMemberInfo xyClubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (xyClubMemberInfo.getIsManager()==1){
                    ArrayList<XyClubMemberInfo> players = clubInfo.getPlayers();
                    List<XYclubPartnerStruct> list = new ArrayList<>();
                    for (XyClubMemberInfo entity : players) {
                        if (entity.getDel_type() == 1 || entity.getIsAccessory()==0) {
                            continue;
                        }
                        UserInfoData infoData = DataManager.getInstance().getMapUserData().get(entity.getUid());
                        XYclubPartnerStruct XYclubPartnerStruct = new XYclubPartnerStruct();
                        XYclubPartnerStruct.setUid(infoData.getId());
                        XYclubPartnerStruct.setPartnerId(entity.getPartner_id());
                        XYclubPartnerStruct.setHeadurl(infoData.getHead());
                        XYclubPartnerStruct.setName(infoData.getName());
                        XYclubPartnerStruct.setBalance_bonus(entity.getBalance_bonus());
                        list.add(XYclubPartnerStruct);
                    }


                    int sumCount = list.size();
                    int sumPage = (int) Math.ceil((double) sumCount/limit);
                    page = page<1?1:page>sumPage?sumPage:page;
                    page = page<1?1:page;
                    int beginIndex = ((page-1)*limit);

                    rsp.setPage(page);
                    rsp.setAllPlayercount(clubInfo.getPartnerCount());
                    rsp.setAllpage(sumPage);
                    rsp.setTotalBonusBalance(clubInfo.getPartnerTheBonusBalance());
                    rsp.setTotalBonusAccumulation(clubInfo.getPartnerDividendsAccumulated()+"");
                    List<XYclubPartnerStruct> sendList = new ArrayList<>();
                    int endIndex = 1;
                    for (int i = 0; i < list.size(); i++) {
                        if (i>=beginIndex && endIndex<=limit){
                            XYclubPartnerStruct userListStructrue = list.get(i);
                            sendList.add(userListStructrue);
                            endIndex++;
                        }
                    }
                    //list转数组
                    XYclubPartnerStruct[] clubUserList = sendList.toArray(new XYclubPartnerStruct[0]);
                    rsp.setPartners(clubUserList);
                }
            }
            client.Send(rsp);
        }
    }

}
